package com.animatedpicture.object;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.Random;

import javax.swing.ImageIcon;

public class Audience implements IMovingObject {
	private Point Pos;
	
	private int max_mt = 3;
	private int ch;
	private boolean ani_Flag = false;
	
	private String headType;
	private ImageIcon headImg;
	
	public Audience(String headType){
		Pos = null;
		ch = new Random().nextInt( max_mt );

		this.headType = headType;
		
		switch (this.headType) {
		case "Default":
			headImg = null;
			break;
		case "Yap Che Een":
			headImg = new ImageIcon("./Image/Yap.png");
			break;
		case "Kim Ik Soo":
			headImg = new ImageIcon("./Image/Kim.png");
			break;
		case "Wong Kok Sheng":
			headImg = new ImageIcon("./Image/Wong.png");
			break;
		case "Shin Yong Tae":
			headImg = new ImageIcon("./Image/Shin.png");
			break;
		case "Kim Seock Yoon":
			headImg = new ImageIcon("./Image/Kim_S.png");
			break;
		default:
			break;
		}
	}

	@Override
	public void draw(Graphics2D g2) {

		if( Pos != null ){
			Ellipse2D.Double HEAD = new Ellipse2D.Double(Pos.x, Pos.y, 30, 30);
			Line2D.Double BODY = new Line2D.Double(Pos.x + 15, Pos.y + 30, Pos.x + 15,
					Pos.y + 90);

			Line2D.Double LEFT_el = new Line2D.Double(Pos.x + 15, Pos.y + 35,
					Pos.x - 15, Pos.y + 30);
			Line2D.Double LEFT_arm = new Line2D.Double(Pos.x - 15, Pos.y + 30,
					Pos.x - 20, Pos.y);

			Line2D.Double RI_el = new Line2D.Double(Pos.x + 15, Pos.y + 35, Pos.x + 35,
					Pos.y + 50);
			Line2D.Double RI_arm = new Line2D.Double(Pos.x + 35, Pos.y + 50, Pos.x + 20,
					Pos.y + 70);
			

			Line2D.Double LEFT_kne = new Line2D.Double(Pos.x + 15, Pos.y + 90, Pos.x ,
					Pos.y + 100);
			Line2D.Double LEFT_fot = new Line2D.Double(Pos.x , Pos.y + 100, Pos.x ,
					Pos.y + 130);
			
			Line2D.Double RI_kne = new Line2D.Double(Pos.x + 15, Pos.y + 90, Pos.x + 30,
					Pos.y + 100);
			Line2D.Double RI_fot = new Line2D.Double(Pos.x + 30, Pos.y + 100, Pos.x + 30,
					Pos.y + 130);
			
			Graphics g = (Graphics)g2;
			
			if( ani_Flag == true ){
				ch = ( ch+1 ) % max_mt;
			}
			
			g2.setColor( Color.black );
			
			if( ch == 0 ){
				HEAD = new Ellipse2D.Double(Pos.x, Pos.y, 30, 30);
				
				if (headImg != null) {
					g.drawImage(headImg.getImage(),Pos.x-5, Pos.y-10, 40, 40, null);
				}
				
				BODY = new Line2D.Double(Pos.x + 15, Pos.y + 30, Pos.x + 15,
						Pos.y + 90);

				LEFT_el = new Line2D.Double(Pos.x + 15, Pos.y + 35,
						Pos.x - 15, Pos.y + 30);
				LEFT_arm = new Line2D.Double(Pos.x - 15, Pos.y + 30,
						Pos.x - 20, Pos.y);

				RI_el = new Line2D.Double(Pos.x + 15, Pos.y + 35, Pos.x + 35,
						Pos.y + 50);
				RI_arm = new Line2D.Double(Pos.x + 35, Pos.y + 50, Pos.x + 20,
						Pos.y + 70);
				

				LEFT_kne = new Line2D.Double(Pos.x + 15, Pos.y + 90, Pos.x ,
						Pos.y + 100);
				LEFT_fot = new Line2D.Double(Pos.x , Pos.y + 100, Pos.x ,
						Pos.y + 130);
				
				RI_kne = new Line2D.Double(Pos.x + 15, Pos.y + 90, Pos.x + 30,
						Pos.y + 100);
				RI_fot = new Line2D.Double(Pos.x + 30, Pos.y + 100, Pos.x + 30,
						Pos.y + 130);
				
				g2.fillOval( Pos.x - 20 - 5, Pos.y - 5, 10, 10);
			}
			else if( ch == 1 ){
				HEAD = new Ellipse2D.Double(Pos.x, Pos.y + 5, 30, 30);
				
				if (headImg != null) {
					g.drawImage(headImg.getImage(),Pos.x-5, Pos.y-10 + 5, 40, 40, null);
				}
				BODY = new Line2D.Double(Pos.x + 15, Pos.y + 30 + 5, Pos.x + 15,
						Pos.y + 100 + 5);

				LEFT_el = new Line2D.Double(Pos.x + 15, Pos.y + 35 + 5,
						Pos.x - 15, Pos.y + 30 + 5);
				LEFT_arm = new Line2D.Double(Pos.x - 15, Pos.y + 30 + 5,
						Pos.x - 20, Pos.y + 5);

				RI_el = new Line2D.Double(Pos.x + 15, Pos.y + 35 + 5, Pos.x + 35,
						Pos.y + 50 + 5);
				RI_arm = new Line2D.Double(Pos.x + 35, Pos.y + 50 + 5, Pos.x + 20,
						Pos.y + 70 + 5);
				

				LEFT_kne = new Line2D.Double(Pos.x + 15, Pos.y + 100 + 5, Pos.x ,
						Pos.y + 100 + 5);
				LEFT_fot = new Line2D.Double(Pos.x , Pos.y + 100 + 5, Pos.x ,
						Pos.y + 130 + 5);
				
				RI_kne = new Line2D.Double(Pos.x + 15, Pos.y + 100 + 5, Pos.x + 30,
						Pos.y + 100 + 5);
				RI_fot = new Line2D.Double(Pos.x + 30, Pos.y + 100 + 5, Pos.x + 30,
						Pos.y + 130 + 5);
				
				g2.fillOval( Pos.x - 20 - 5, Pos.y - 5, 10, 10);
			}
			else if( ch == 2 ){
				HEAD = new Ellipse2D.Double(Pos.x, Pos.y, 30, 30);
				if (headImg != null) {
					g.drawImage(headImg.getImage(),Pos.x-5, Pos.y-10, 40, 40, null);
				}
				BODY = new Line2D.Double(Pos.x + 15, Pos.y + 30, Pos.x + 15,
						Pos.y + 90);

				LEFT_el = new Line2D.Double(Pos.x + 15, Pos.y + 35,
						Pos.x - 15, Pos.y + 30);
				LEFT_arm = new Line2D.Double(Pos.x - 15, Pos.y + 30,
						Pos.x - 20 + 5, Pos.y);

				RI_el = new Line2D.Double(Pos.x + 15, Pos.y + 35, Pos.x + 35,
						Pos.y + 50);
				RI_arm = new Line2D.Double(Pos.x + 35, Pos.y + 50, Pos.x + 20,
						Pos.y + 70);
				

				LEFT_kne = new Line2D.Double(Pos.x + 15, Pos.y + 90, Pos.x ,
						Pos.y + 100);
				LEFT_fot = new Line2D.Double(Pos.x , Pos.y + 100, Pos.x ,
						Pos.y + 130);
				
				RI_kne = new Line2D.Double(Pos.x + 15, Pos.y + 90, Pos.x + 30,
						Pos.y + 100);
				RI_fot = new Line2D.Double(Pos.x + 30, Pos.y + 100, Pos.x + 30,
						Pos.y + 130);
				
				g2.fillOval( Pos.x - 20 - 5 + 5, Pos.y - 5, 10, 10);
			}
			else if( ch == 3 ){
				HEAD = new Ellipse2D.Double(Pos.x, Pos.y + 5, 30, 30);
				if (headImg != null) {
					g.drawImage(headImg.getImage(),Pos.x-5, Pos.y-10 + 5, 40, 40, null);
				}
				BODY = new Line2D.Double(Pos.x + 15, Pos.y + 30 + 5, Pos.x + 15,
						Pos.y + 100 + 5);

				LEFT_el = new Line2D.Double(Pos.x + 15, Pos.y + 35 + 5,
						Pos.x - 15, Pos.y + 30 + 5);
				LEFT_arm = new Line2D.Double(Pos.x - 15, Pos.y + 30 + 5,
						Pos.x - 20, Pos.y + 5);

				RI_el = new Line2D.Double(Pos.x + 15, Pos.y + 35 + 5, Pos.x + 35,
						Pos.y + 50 + 5);
				RI_arm = new Line2D.Double(Pos.x + 35, Pos.y + 50 + 5, Pos.x + 20,
						Pos.y + 70 + 5);
				

				LEFT_kne = new Line2D.Double(Pos.x + 15, Pos.y + 100 + 5, Pos.x ,
						Pos.y + 100 + 5);
				LEFT_fot = new Line2D.Double(Pos.x , Pos.y + 100 + 5, Pos.x ,
						Pos.y + 130 + 5);
				
				RI_kne = new Line2D.Double(Pos.x + 15, Pos.y + 100 + 5, Pos.x + 30,
						Pos.y + 100 + 5);
				RI_fot = new Line2D.Double(Pos.x + 30, Pos.y + 100 + 5, Pos.x + 30,
						Pos.y + 130 + 5);
				
				g2.fillOval( Pos.x - 20 - 5, Pos.y - 5, 10, 10);
			}

			g2.setStroke( new BasicStroke( 3 ) );
			g2.setColor( Color.blue );

			if (headImg == null) {
				g2.fill( HEAD );
				g2.draw(HEAD);
			}
			g2.draw(BODY);
			g2.draw(LEFT_arm);
			g2.draw(LEFT_el);
			g2.draw(RI_arm);
			g2.draw(RI_el);

			g2.draw(LEFT_fot);
			g2.draw(LEFT_kne);
			g2.draw(RI_fot);
			g2.draw(RI_kne);
		}
	}

	public void setPosition(Point mPoint) {
		// TODO Auto-generated method stub

		Pos = mPoint;
		System.out.println("ObjectCar SetPosition : ("+mPoint.x+","+mPoint.y+")");
	}

	@Override
	public void animate(boolean trriger) {
		// TODO Auto-generated method stub
		ani_Flag = trriger;
		
	}

	@Override
	public void playMusic(boolean trriger) {
		// TODO Auto-generated method stub
		
	}
}
